package com.txzt.gaea.business.sequence.service.impl;

import com.txzt.gaea.business.sequence.domain.model.Sequence;
import com.txzt.gaea.business.sequence.service.ISequenceService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class SequenceServiceImpl extends BaseServiceImpl<Sequence> implements ISequenceService {

    @Override
    public Integer getSequence(String bizName, String name) {
        Sequence res = sqlManager.lambdaQuery(Sequence.class).andEq(Sequence::getBizName,bizName).andEq(Sequence::getName,name).single();
        if (null == res) {
            Sequence target = new Sequence(bizName,name,1);
            sqlManager.insertTemplate(target);
            return 1;
        }else{
            Integer sequence = res.getSequence();
            if (sequence >= 999) {
                res.setSequence(1);
            }else {
                res.setSequence(sequence + 1);
            }
            sqlManager.updateTemplateById(res);
            return sequence;
        }
    }
}
